const utils = require("../../utils/util");
const cloud = require("../../utils/api.js");
const {
  getCloudFunctionData
} = require("../../utils/cloudRequest.js");
const app = getApp();

// pages/blogs/blogs.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    hide: app.globalData.hide,
    searchIpt: "",
    placeholder: "请输入关键词搜索",
    sureSearch: false,
    searchBlogsList: [],
    choicenessBlogs: [],
    page: 1,
    pageSize: 20,
    blogsList: [],
    classify: [],
    scrollTop: 0,
    moreList: true,
    loading: true
  },

  async getBlogsData() {
    let {
      page,
      pageSize,
      blogsList,
      choicenessBlogs
    } = this.data;
    const res = await getCloudFunctionData({
      gather: 'blogs',
      page,
      pageSize,
      order: {
        name: 'time',
        sort: 'desc'
      }
    })
    if (res.data && res.data.length > 0) {
      page++;
      blogsList = blogsList.concat(res.data);
      this.setClassify(blogsList);
      choicenessBlogs = blogsList.filter((i) => i.choiceness)
      this.setData({
        page,
        blogsList,
        choicenessBlogs
      })
    } else if (blogsList.length != 0 && res.data.length <= 0) {
      wx.showToast({
        title: '没有更多了~',
        icon: 'none'
      })
    }
  },

  getBlogsList(skip) {
    const that = this;
    cloud.cloudGet("blogs", {}, true, 20, skip).then((res) => {
      if (res.length == 0) {
        that.setData({
          moreList: false,
          page: 0,
        });
        wx.showToast({
          title: "没有更多了",
          icon: "none",
        });
      }
      let {
        blogsList,
        choicenessBlogs
      } = that.data;
      let list = res;
      that.setClassify(list);
      blogsList = blogsList.concat(utils.reSortArr(list));
      choicenessBlogs = choicenessBlogs.concat(
        utils.reSortArr(list.filter((i) => i.choiceness))
      );
      that.setData({
          blogsList,
          choicenessBlogs,
          loading: false
        },
        () => {
          wx.hideLoading();
        }
      );
    });
  },

  setClassify(arr) {
    let {
      classify
    } = this.data;
    arr.forEach((i) => classify.push(i.classify));
    classify = Array.from(new Set(classify));
    classify = utils.reSortArr(classify);
    this.setData({
      classify,
    });
  },

  onSearch(e) {
    const {
      list,
      search
    } = e.detail;
    this.setData({
      sureSearch: search,
      searchBlogsList: list,
      //   search
    });
  },

  handleInput(e) {
    const {
      value
    } = e.detail;
    this.setData({
      searchIpt: value,
    });
  },

  searchBlogs() {
    const that = this;
    let {
      searchIpt,
      blogsList,
      searchBlogsList
    } = that.data;
    if (searchIpt == "") {
      return;
    }
    that.setData({
        placeholder: searchIpt,
      },
      () => {
        searchBlogsList = blogsList.filter(
          (i) => i.title.indexOf(searchIpt) >= 0
        );
        that.setData({
          searchBlogsList,
          sureSearch: true,
          searchIpt: "",
        });
      }
    );
  },

  closeSearch() {
    const mysearch = this.selectComponent("#mysearch");
    mysearch.cancelSearch();
    this.setData({
      sureSearch: false,
      searchIpt: "",
      placeholder: "请输入关键词搜索",
      searchBlogsList: [],
    });
  },

  goDetail(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/blogs/detail/detail?id=" + item._id + "&title=" + item.title + "&type=" + (item.type ? item.type : "markdown"),
    });
  },

  allBlogs() {
    wx.navigateTo({
      url: "/pages/blogs/all/all",
    });
  },

  allClassify() {
    wx.showToast({
      title: "开发中",
      icon: "error",
    });
  },

  goClassify(e) {
    const {
      type
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: "/pages/blogs/classify/classify?type=" + type,
    });
  },

  scrollTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300,
    });
  },
  onPageScroll(e) {
    this.setData({
      scrollTop: e.scrollTop,
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const hide = app.globalData.hide;
    this.setData({
      hide
    });
    // if (hide) {
    //   this.setData({
    //     loading: false
    //   })
    //   return;
    // };
    // this.getBlogsList();
    this.getBlogsData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
        themeType: app.globalData.themeType,
      });
    }

    this.setData({
      themeType: app.globalData.themeType,
    });

    const themeType = wx.getStorageSync("themeType");
    if (themeType == "dark") {
      wx.setNavigationBarColor({
        backgroundColor: "#121212",
        frontColor: "#ffffff",
      });
      wx.setBackgroundColor({
        backgroundColor: '#121212', // 窗口的背景色
        backgroundColorTop: '#121212', // 顶部窗口的背景色
        backgroundColorBottom: '#121212', // 底部窗口的背景色
      })
    } else {
      wx.setNavigationBarColor({
        backgroundColor: "#ffffff",
        frontColor: "#000000",
      });
      wx.setBackgroundColor({
        backgroundColor: '#ffffff', // 窗口的背景色
        backgroundColorTop: '#f6f7f9', // 顶部窗口的背景色
        backgroundColorBottom: '#f6f7f9', // 底部窗口的背景色
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getBlogsList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    let {
      page,
      moreList
    } = that.data;
    if (moreList == false) return;
    page += 20;
    that.setData({
      page,
    });
    this.getBlogsList(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  // 分享到朋友圈
  onShareTimeline() {},
});